﻿namespace HRM.CORE
{
    using DevExpress.Utils;
    using DevExpress.XtraBars;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraGrid;
    using DevExpress.XtraGrid.Columns;
    using DevExpress.XtraGrid.Views.Grid;
    using HRM.CORE;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;
    using DevExpress.XtraGrid.Views.Base;

    public class xucSalaryPay1 : UserControl
    {
        private BarDockControl barDockControlBottom;
        private BarDockControl barDockControlLeft;
        private BarDockControl barDockControlRight;
        private BarDockControl barDockControlTop;
        private BarManager barManager1;
        private BarButtonItem bbiDelete;
        private GridColumn colDate;
        private GridColumn colDescription;
        private GridColumn colEmployeeCode;
        private GridColumn colMoney;
        private GridColumn colOrder;
        private GridColumn colPayMoney;
        private GridColumn colPerson;
        private GridColumn colRealMoney;
        private GridColumn colReason;
        private GridColumn colSalaryPayID;
        private GridColumn colSalaryTableListID;
        private IContainer components;
        private GridView gbList;
        private GridControl gcList;
        private string m_EmployeeCode;
        private decimal m_Money;
        private decimal m_PayMoney;
        private decimal m_RealMoney;
        private Guid m_SalaryPayID;
        private string m_SalaryRewardName;
        private Guid m_SalaryTableListID;
        private PopupMenu ppMenu;
        private RepositoryItemDateEdit repDate;
        private RepositoryItemCalcEdit repMoney;
        private RepositoryItemCalcEdit repPayMoney;
        private RepositoryItemCalcEdit repRealMoney;

        public event SavedHandler Saved;

        public xucSalaryPay1()
        {
            this.m_SalaryTableListID = Guid.Empty;
            this.m_SalaryPayID = Guid.Empty;
            this.m_SalaryRewardName = "";
            this.m_Money = 0M;
            this.m_PayMoney = 0M;
            this.m_RealMoney = 0M;
            this.m_EmployeeCode = "";
            this.components = null;
            this.InitializeComponent();
        }

        public xucSalaryPay1(Guid SalaryTableListID, string EmployeeCode)
        {
            this.m_SalaryTableListID = Guid.Empty;
            this.m_SalaryPayID = Guid.Empty;
            this.m_SalaryRewardName = "";
            this.m_Money = 0M;
            this.m_PayMoney = 0M;
            this.m_RealMoney = 0M;
            this.m_EmployeeCode = "";
            this.components = null;
            this.InitializeComponent();
            this.Init(this.m_SalaryTableListID = SalaryTableListID, this.m_EmployeeCode = EmployeeCode);
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.gbList.DeleteSelectedRows();
        }

        public void ClearData()
        {
            this.gcList.DataSource = null;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void gbList_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.RowHandle == -2147483648)
            {
                e.Handled = true;
                e.Painter.DrawObject(e.Info);
                Rectangle bounds = e.Bounds;
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0x3f, 0xa5, 0xa5, 0)), bounds);
                bounds.Height--;
                bounds.Width--;
                e.Graphics.DrawRectangle(Pens.Blue, bounds);
            }
            int rowHandle = e.RowHandle;
            if (rowHandle >= 0)
            {
                rowHandle++;
                e.Info.DisplayText = rowHandle.ToString();
            }
        }

        private void gbList_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            this.gbList.SetRowCellValue(e.RowHandle, this.colDate, DateTime.Now);
            this.gbList.SetRowCellValue(e.RowHandle, this.colMoney, 0);
            this.gbList.SetRowCellValue(e.RowHandle, this.colPayMoney, 0);
            this.gbList.SetRowCellValue(e.RowHandle, this.colRealMoney, 0);
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.gbList.DeleteSelectedRows();
            }
            if (e.KeyCode == Keys.F5)
            {
                this.Init(this.m_SalaryTableListID, this.m_EmployeeCode);
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            }
        }

        private void gbList_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        public void Init(Guid SalaryTableListID, string EmployeeCode)
        {
            this.m_Money = 0M;
            this.m_PayMoney = 0M;
            this.m_RealMoney = 0M;
            this.barManager1.SetPopupContextMenu(this.gcList, this.ppMenu);
            this.repMoney.EditValueChanging += new ChangingEventHandler(this.repMoney_EditValueChanging);
            this.repMoney.ParseEditValue += new ConvertEditValueEventHandler(this.repMoney_ParseEditValue);
            this.repPayMoney.EditValueChanging += new ChangingEventHandler(this.repPayMoney_EditValueChanging);
            this.repPayMoney.ParseEditValue += new ConvertEditValueEventHandler(this.repPayMoney_ParseEditValue);
            this.gcList.DataSource = new HRM_SALARY_PAY().GetList(this.m_SalaryTableListID = SalaryTableListID, this.m_EmployeeCode = EmployeeCode);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            this.gcList = new GridControl();
            this.gbList = new GridView();
            this.colSalaryTableListID = new GridColumn();
            this.colEmployeeCode = new GridColumn();
            this.colSalaryPayID = new GridColumn();
            this.colOrder = new GridColumn();
            this.colReason = new GridColumn();
            this.colDate = new GridColumn();
            this.repDate = new RepositoryItemDateEdit();
            this.colMoney = new GridColumn();
            this.repMoney = new RepositoryItemCalcEdit();
            this.colPayMoney = new GridColumn();
            this.repPayMoney = new RepositoryItemCalcEdit();
            this.colRealMoney = new GridColumn();
            this.repRealMoney = new RepositoryItemCalcEdit();
            this.colPerson = new GridColumn();
            this.colDescription = new GridColumn();
            this.barManager1 = new BarManager(this.components);
            this.barDockControlTop = new BarDockControl();
            this.barDockControlBottom = new BarDockControl();
            this.barDockControlLeft = new BarDockControl();
            this.barDockControlRight = new BarDockControl();
            this.bbiDelete = new BarButtonItem();
            this.ppMenu = new PopupMenu(this.components);
            this.gcList.BeginInit();
            this.gbList.BeginInit();
            this.repDate.BeginInit();
            this.repDate.VistaTimeProperties.BeginInit();
            this.repMoney.BeginInit();
            this.repPayMoney.BeginInit();
            this.repRealMoney.BeginInit();
            this.barManager1.BeginInit();
            this.ppMenu.BeginInit();
            base.SuspendLayout();
            this.gcList.Dock = DockStyle.Fill;
            this.gcList.EmbeddedNavigator.Buttons.Append.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.CancelEdit.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.Edit.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.EndEdit.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.Remove.Visible = false;
            this.gcList.EmbeddedNavigator.TextStringFormat = "{0}/{1}";
            this.gcList.Location = new Point(0, 0);
            this.gcList.MainView = this.gbList;
            this.gcList.Name = "gcList";
            this.gcList.RepositoryItems.AddRange(new RepositoryItem[] { this.repMoney, this.repDate, this.repPayMoney, this.repRealMoney });
            this.gcList.Size = new Size(0x310, 0x192);
            this.gcList.TabIndex = 5;
            this.gcList.UseEmbeddedNavigator = true;
            this.gcList.ViewCollection.AddRange(new BaseView[] { this.gbList });
            this.gbList.Appearance.GroupRow.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            this.gbList.Appearance.GroupRow.Options.UseFont = true;
            this.gbList.BorderStyle = BorderStyles.NoBorder;
            this.gbList.Columns.AddRange(new GridColumn[] { this.colSalaryTableListID, this.colEmployeeCode, this.colSalaryPayID, this.colOrder, this.colReason, this.colDate, this.colMoney, this.colPayMoney, this.colRealMoney, this.colPerson, this.colDescription });
            this.gbList.GridControl = this.gcList;
            this.gbList.GroupPanelText = "K\x00e9o v\x00e0 Thả cột v\x00e0o đ\x00e2y để nh\x00f3m dữ liệu";
            this.gbList.IndicatorWidth = 40;
            this.gbList.Name = "gbList";
            this.gbList.OptionsBehavior.AllowIncrementalSearch = true;
            this.gbList.OptionsBehavior.AutoExpandAllGroups = true;
            this.gbList.OptionsBehavior.CopyToClipboardWithColumnHeaders = false;
            this.gbList.OptionsBehavior.FocusLeaveOnTab = true;
            this.gbList.OptionsLayout.StoreAllOptions = true;
            this.gbList.OptionsLayout.StoreAppearance = true;
            this.gbList.OptionsMenu.EnableColumnMenu = false;
            this.gbList.OptionsMenu.EnableGroupPanelMenu = false;
            this.gbList.OptionsNavigation.EnterMoveNextColumn = true;
            this.gbList.OptionsSelection.InvertSelection = true;
            this.gbList.OptionsSelection.MultiSelect = true;
            this.gbList.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CellSelect;
            this.gbList.OptionsView.ColumnAutoWidth = false;
            this.gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            this.gbList.OptionsView.RowAutoHeight = true;
            this.gbList.OptionsView.ShowGroupPanel = false;
            this.gbList.CustomDrawRowIndicator += new RowIndicatorCustomDrawEventHandler(this.gbList_CustomDrawRowIndicator);
            this.gbList.InitNewRow += new InitNewRowEventHandler(this.gbList_InitNewRow);
            this.gbList.KeyDown += new KeyEventHandler(this.gbList_KeyDown);
            this.colSalaryTableListID.Caption = "M\x00e3 bảng lương";
            this.colSalaryTableListID.FieldName = "SalaryTableListID";
            this.colSalaryTableListID.Name = "colSalaryTableListID";
            this.colEmployeeCode.Caption = "M\x00e3 nh\x00e2n vi\x00ean";
            this.colEmployeeCode.FieldName = "EmployeeCode";
            this.colEmployeeCode.Name = "colEmployeeCode";
            this.colEmployeeCode.OptionsColumn.AllowEdit = false;
            this.colEmployeeCode.OptionsColumn.ReadOnly = true;
            this.colEmployeeCode.Width = 0x4d;
            this.colSalaryPayID.Caption = "M\x00e3 thanh to\x00e1n";
            this.colSalaryPayID.FieldName = "SalaryPayID";
            this.colSalaryPayID.Name = "colSalaryPayID";
            this.colSalaryPayID.Width = 0x75;
            this.colOrder.AppearanceCell.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            this.colOrder.AppearanceCell.Options.UseFont = true;
            this.colOrder.AppearanceCell.Options.UseTextOptions = true;
            this.colOrder.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            this.colOrder.Caption = "Lần thanh to\x00e1n";
            this.colOrder.FieldName = "Order";
            this.colOrder.Name = "colOrder";
            this.colOrder.Visible = true;
            this.colOrder.VisibleIndex = 0;
            this.colOrder.Width = 0x66;
            this.colReason.Caption = "L\x00fd do";
            this.colReason.FieldName = "Reason";
            this.colReason.Name = "colReason";
            this.colReason.Width = 340;
            this.colDate.Caption = "Ng\x00e0y thanh to\x00e1n";
            this.colDate.ColumnEdit = this.repDate;
            this.colDate.FieldName = "Date";
            this.colDate.Name = "colDate";
            this.colDate.Visible = true;
            this.colDate.VisibleIndex = 1;
            this.colDate.Width = 0x6a;
            this.repDate.AutoHeight = false;
            this.repDate.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.repDate.Mask.EditMask = "dd/MM/yyyy";
            this.repDate.Name = "repDate";
            this.repDate.VistaTimeProperties.Buttons.AddRange(new EditorButton[] { new EditorButton() });
            this.colMoney.Caption = "Số tiền (VND)";
            this.colMoney.ColumnEdit = this.repMoney;
            this.colMoney.FieldName = "Money";
            this.colMoney.Name = "colMoney";
            this.colMoney.Visible = true;
            this.colMoney.VisibleIndex = 2;
            this.colMoney.Width = 0x61;
            this.repMoney.AutoHeight = false;
            this.repMoney.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.repMoney.Mask.UseMaskAsDisplayFormat = true;
            this.repMoney.Name = "repMoney";
            this.colPayMoney.Caption = "Nộp lại";
            this.colPayMoney.ColumnEdit = this.repPayMoney;
            this.colPayMoney.FieldName = "PayMoney";
            this.colPayMoney.Name = "colPayMoney";
            this.colPayMoney.Visible = true;
            this.colPayMoney.VisibleIndex = 3;
            this.colPayMoney.Width = 0x59;
            this.repPayMoney.AutoHeight = false;
            this.repPayMoney.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.repPayMoney.Mask.UseMaskAsDisplayFormat = true;
            this.repPayMoney.Name = "repPayMoney";
            this.colRealMoney.Caption = "Thực thanh to\x00e1n";
            this.colRealMoney.ColumnEdit = this.repRealMoney;
            this.colRealMoney.FieldName = "RealMoney";
            this.colRealMoney.Name = "colRealMoney";
            this.colRealMoney.Visible = true;
            this.colRealMoney.VisibleIndex = 4;
            this.colRealMoney.Width = 0x67;
            this.repRealMoney.AutoHeight = false;
            this.repRealMoney.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.repRealMoney.Mask.UseMaskAsDisplayFormat = true;
            this.repRealMoney.Name = "repRealMoney";
            this.colPerson.Caption = "Người thanh to\x00e1n";
            this.colPerson.FieldName = "Person";
            this.colPerson.Name = "colPerson";
            this.colPerson.Visible = true;
            this.colPerson.VisibleIndex = 5;
            this.colPerson.Width = 0x6c;
            this.colDescription.Caption = "Ghi ch\x00fa";
            this.colDescription.FieldName = "Description";
            this.colDescription.Name = "colDescription";
            this.colDescription.Visible = true;
            this.colDescription.VisibleIndex = 6;
            this.colDescription.Width = 0x111;
            this.barManager1.DockControls.Add(this.barDockControlTop);
            this.barManager1.DockControls.Add(this.barDockControlBottom);
            this.barManager1.DockControls.Add(this.barDockControlLeft);
            this.barManager1.DockControls.Add(this.barDockControlRight);
            this.barManager1.Form = this;
            this.barManager1.Items.AddRange(new BarItem[] { this.bbiDelete });
            this.barManager1.MaxItemId = 1;
            this.bbiDelete.Caption = "X\x00f3a";
            this.bbiDelete.Id = 0;
            this.bbiDelete.Name = "bbiDelete";
            this.bbiDelete.ItemClick += new ItemClickEventHandler(this.bbiDelete_ItemClick);
            this.ppMenu.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(this.bbiDelete) });
            this.ppMenu.Manager = this.barManager1;
            this.ppMenu.Name = "ppMenu";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.gcList);
            base.Controls.Add(this.barDockControlLeft);
            base.Controls.Add(this.barDockControlRight);
            base.Controls.Add(this.barDockControlBottom);
            base.Controls.Add(this.barDockControlTop);
            base.Name = "xucSalaryPay1";
            base.Size = new Size(0x310, 0x192);
            this.gcList.EndInit();
            this.gbList.EndInit();
            this.repDate.VistaTimeProperties.EndInit();
            this.repDate.EndInit();
            this.repMoney.EndInit();
            this.repPayMoney.EndInit();
            this.repRealMoney.EndInit();
            this.barManager1.EndInit();
            this.ppMenu.EndInit();
            base.ResumeLayout(false);
        }

        private void RaiseSavedHander()
        {
            if (this.Saved != null)
            {
                this.Saved(this);
            }
        }

        private void repMoney_EditValueChanging(object sender, ChangingEventArgs e)
        {
            try
            {
                this.m_Money = decimal.Parse(e.NewValue.ToString());
            }
            catch
            {
            }
        }

        private void repMoney_ParseEditValue(object sender, ConvertEditValueEventArgs e)
        {
            try
            {
                this.m_PayMoney = decimal.Parse(this.gbList.GetFocusedRowCellValue(this.colPayMoney).ToString());
                this.gbList.SetFocusedRowCellValue(this.colMoney, this.m_Money);
                this.gbList.SetFocusedRowCellValue(this.colRealMoney, this.m_Money - this.m_PayMoney);
            }
            catch
            {
            }
        }

        private void repPayMoney_EditValueChanging(object sender, ChangingEventArgs e)
        {
            try
            {
                this.m_PayMoney = decimal.Parse(e.NewValue.ToString());
            }
            catch
            {
            }
        }

        private void repPayMoney_ParseEditValue(object sender, ConvertEditValueEventArgs e)
        {
            try
            {
                this.m_Money = decimal.Parse(this.gbList.GetFocusedRowCellValue(this.colMoney).ToString());
                this.gbList.SetFocusedRowCellValue(this.colPayMoney, this.m_PayMoney);
                this.gbList.SetFocusedRowCellValue(this.colRealMoney, this.m_Money - this.m_PayMoney);
            }
            catch
            {
            }
        }

        public void Save()
        {
            HRM_SALARY_PAY hrm_salary_pay = new HRM_SALARY_PAY();
            hrm_salary_pay.Delete(this.m_SalaryTableListID, this.m_EmployeeCode);
            try
            {
                foreach (DataRow row in (this.gcList.DataSource as DataTable).Rows)
                {
                    try
                    {
                        if (row != null)
                        {
                            hrm_salary_pay.Insert(this.m_SalaryTableListID, this.m_EmployeeCode, Guid.NewGuid(), int.Parse(row["Order"].ToString()), row["Reason"].ToString(), DateTime.Parse(row["Date"].ToString()), decimal.Parse(row["Money"].ToString()), decimal.Parse(row["PayMoney"].ToString()), decimal.Parse(row["RealMoney"].ToString()), row["Person"].ToString(), row["Description"].ToString());
                        }
                    }
                    catch
                    {
                    }
                }
                this.RaiseSavedHander();
            }
            catch
            {
            }
        }

        private void UpdateEmployeeSalary()
        {
            HRM_TIMEKEEPER_TABLELIST hrm_timekeeper_tablelist = new HRM_TIMEKEEPER_TABLELIST();
            HRM_SALARY_TABLELIST hrm_salary_tablelist = new HRM_SALARY_TABLELIST();
            hrm_salary_tablelist.GetByID(this.m_SalaryTableListID);
            hrm_timekeeper_tablelist.Get(hrm_salary_tablelist.Month, hrm_salary_tablelist.Year);
            HRM_SALARY.EmployeeUpdate(hrm_timekeeper_tablelist.TimeKeeperTableListID.ToString(), this.m_SalaryTableListID.ToString(), hrm_salary_tablelist.Month, hrm_salary_tablelist.Year, this.m_EmployeeCode);
        }

        public delegate void SavedHandler(object sender);
    }
}

